package com.winhxd.b2c.order.api;

import com.winhxd.b2c.common.constant.BusinessCode;
import com.winhxd.b2c.common.context.CustomerUser;
import com.winhxd.b2c.common.context.UserContext;
import com.winhxd.b2c.common.domain.PagedList;
import com.winhxd.b2c.common.domain.ResponseResult;
import com.winhxd.b2c.common.domain.order.condition.OrderProfitSummaryByLevelCondition;
import com.winhxd.b2c.common.domain.order.condition.OrderProfitSummaryCondition;
import com.winhxd.b2c.common.domain.order.condition.OrderSalesSummaryCondition;
import com.winhxd.b2c.common.domain.order.condition.OrderSummaryCondition;
import com.winhxd.b2c.common.domain.order.vo.Order4meSummaryVO;
import com.winhxd.b2c.common.domain.order.vo.OrderDistributionOneSummaryVO;
import com.winhxd.b2c.common.domain.order.vo.OrderDistributionTwoSummaryVO;
import com.winhxd.b2c.common.domain.order.vo.OrderProfitVO;
import com.winhxd.b2c.common.exception.BusinessException;
import com.winhxd.b2c.common.util.JsonUtil;
import com.winhxd.b2c.order.service.OrderDataStatisticsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author lijianhua
 * @Description //订单数据统计
 * @Date 14:25 2019/1/7
 **/
@RestController
@Api(tags = "ApiOrderDataStatistics")
@RequestMapping(value = "/api-order/order")
public class ApiOrderDataStatisticsController {
    private static final Logger LOGGER = LoggerFactory.getLogger(ApiOrderDataStatisticsController.class);
    @Autowired
    private OrderDataStatisticsService orderDataStatisticsService;

    public final int LEVEL_0 = 0;
    public final int LEVEL_1 = 1;
    public final int LEVEL_2 = 2;
    private  final int DEFAULT_PAGE=1;
    private  final int DEFAULT_ZERO=0;
    private  final int DEFAULT_PAGE_SIZE=20;

    @ApiOperation(value = "订单查询数据", response = Order4meSummaryVO.class, notes = "订单查询数据")
    @RequestMapping(value = "/4800/v1/getSummarySummary", method = RequestMethod.POST)
    public ResponseResult<Order4meSummaryVO> getSummarySummary(@RequestBody OrderSummaryCondition condition) {
        String logTitle = "/api-order/order/4800/v1/getSummarySummary-会员我的订单查询数据";
        LOGGER.info("{}=--开始--{}", logTitle, JsonUtil.toJSONString(condition));
        ResponseResult<Order4meSummaryVO> result = new ResponseResult<>();

        switch (condition.getUserLevel()) {
            case LEVEL_0:
                result = get4meSummarySummary(condition);
                break;
            case LEVEL_1:
                result = get4OneSummarySummary(condition);
                break;
            case LEVEL_2:
                result = get4TwoSummarySummary(condition);
                break;
            default:
                BusinessException e = new BusinessException();
                e.setErrorCode(BusinessCode.CODE_1001);
                throw e;
        }

        LOGGER.info("{}=--结束 result={}", logTitle, JsonUtil.toJSONString(result));
        return result;
    }

    @ApiOperation(value = "会员我的订单查询数据", response = Order4meSummaryVO.class, notes = "会员我的订单查询数据")
    @RequestMapping(value = "/4801/v1/get4meSummarySummary", method = RequestMethod.POST)
    public ResponseResult<Order4meSummaryVO> get4meSummarySummary(@RequestBody OrderSalesSummaryCondition condition) {
        String logTitle = "/api-order/order/4801/v1/get4meSummarySummary-会员我的订单查询数据";
        LOGGER.info("{}=--开始--{}", logTitle, JsonUtil.toJSONString(condition));
        CustomerUser customer = UserContext.getCurrentCustomerUser();
        condition.setCustomerId(customer.getCustomerId());
        if (customer == null || customer.getCustomerId()==null) {
            throw new BusinessException(BusinessCode.CODE_1026);
        }
        ResponseResult<Order4meSummaryVO> result = new ResponseResult<>();

        Order4meSummaryVO vo = orderDataStatisticsService.get4meSummarySummary(condition);
        result.setData(vo);

        LOGGER.info("{}=--结束 result={}", logTitle, JsonUtil.toJSONString(result));
        return result;
    }

    @ApiOperation(value = "一级分销我的订单查询数据", response = Order4meSummaryVO.class, notes = "一级分销我的订单查询数据")
    @RequestMapping(value = "/4802/v1/get4OneSummarySummary", method = RequestMethod.POST)
    public ResponseResult<Order4meSummaryVO> get4OneSummarySummary(@RequestBody OrderSalesSummaryCondition condition) {
        String logTitle = "/api-order/order/4802/v1/get4OneSummarySummary-一级分销我的订单查询数据";
        LOGGER.info("{}=--开始--{}", logTitle, JsonUtil.toJSONString(condition));
        CustomerUser customer = UserContext.getCurrentCustomerUser();
        condition.setCustomerId(customer.getCustomerId());
        if (customer == null || customer.getCustomerId()==null) {
            throw new BusinessException(BusinessCode.CODE_1026);
        }
        ResponseResult<Order4meSummaryVO> result = new ResponseResult<>();

        Order4meSummaryVO vo = orderDataStatisticsService.get4OneSummarySummary(condition);
        result.setData(vo);

        LOGGER.info("{}=--结束 result={}", logTitle, JsonUtil.toJSONString(result));
        return result;
    }

    @ApiOperation(value = "二级分销我的订单查询数据", response = Order4meSummaryVO.class, notes = "二级分销我的订单查询数据")
    @RequestMapping(value = "/4803/v1/get4TwoSummarySummary", method = RequestMethod.POST)
    public ResponseResult<Order4meSummaryVO> get4TwoSummarySummary(@RequestBody OrderSalesSummaryCondition condition) {
        String logTitle = "/api-order/order/4803/v1/get4TwoSummarySummary-二级分销我的订单查询数据";
        LOGGER.info("{}=--开始--{}", logTitle, JsonUtil.toJSONString(condition));
        CustomerUser customer = UserContext.getCurrentCustomerUser();
        condition.setCustomerId(customer.getCustomerId());
        if (customer == null || customer.getCustomerId()==null) {
            throw new BusinessException(BusinessCode.CODE_1026);
        }
        ResponseResult<Order4meSummaryVO> result = new ResponseResult<>();

        Order4meSummaryVO vo = orderDataStatisticsService.get4TwoSummarySummary(condition);
        result.setData(vo);

        LOGGER.info("{}=--结束 result={}", logTitle, JsonUtil.toJSONString(result));
        return result;
    }


    @ApiOperation(value = "一级分销商查询数据", response = OrderDistributionOneSummaryVO.class, notes = "一级分销商查询数据")
    @RequestMapping(value = "/4804/v1/getOrderDistributionOneSummary", method = RequestMethod.POST)
    public ResponseResult<OrderDistributionOneSummaryVO> getOrderDistributionOneSummary(@RequestBody OrderSalesSummaryCondition condition) {
        String logTitle = "/api-order/order/4804/v1/getOrderDistributionOneSummary-一级分销商查询数据";
        LOGGER.info("{}=--开始--{}", logTitle, JsonUtil.toJSONString(condition));
        CustomerUser customer = UserContext.getCurrentCustomerUser();
        condition.setCustomerId(customer.getCustomerId());
        if (customer == null || customer.getCustomerId()==null) {
            throw new BusinessException(BusinessCode.CODE_1026);
        }
        ResponseResult<OrderDistributionOneSummaryVO> result = new ResponseResult<>();
        condition.setLevel(customer.getLevel());
        OrderDistributionOneSummaryVO vo = orderDataStatisticsService.getOrderDistributionOneSummary(condition);
        result.setData(vo);

        LOGGER.info("{}=--结束 result={}", logTitle, JsonUtil.toJSONString(result));
        return result;
    }


    @ApiOperation(value = "一级分销商总收益列表", response = OrderProfitVO.class, notes = "一级分销商总收益列表")
    @RequestMapping(value = "/4805/v1/findDistOneOrderProfitSummary", method = RequestMethod.POST)
    public  ResponseResult<PagedList<OrderProfitVO>> findDistOneOrderProfitSummary(@RequestBody OrderProfitSummaryCondition condition) {
        String logTitle = "/api-order/order/4805/v1/findDistOneOrderProfitSummary-一级分销商总收益列表";
        LOGGER.info("{}=--开始--{}", logTitle, JsonUtil.toJSONString(condition));
        CustomerUser customer = UserContext.getCurrentCustomerUser();
        condition.setCustomerId(customer.getCustomerId());
        if (customer == null || customer.getCustomerId()==null) {
            throw new BusinessException(BusinessCode.CODE_1026);
        }

        //默认从第1页开始查询，每页20条
        if (condition.getPageNo() == DEFAULT_ZERO) {
            condition.setPageNo(DEFAULT_PAGE);
        }
        if (condition.getPageSize() == DEFAULT_ZERO) {
            condition.setPageSize(DEFAULT_PAGE_SIZE);
        }
        ResponseResult<PagedList<OrderProfitVO>> result = new ResponseResult<>();

        PagedList<OrderProfitVO> vo = orderDataStatisticsService.findDistOneOrderProfitSummary(condition);
        result.setData(vo);

        LOGGER.info("{}=--结束 result={}", logTitle, JsonUtil.toJSONString(result));
        return result;
    }

    @ApiOperation(value = "一级分销商线下收益列表", response = OrderProfitVO.class, notes = "一级分销商线下收益列表")
    @RequestMapping(value = "/4806/v1/findDistOneOrderReferralsProfitSummary", method = RequestMethod.POST)
    public ResponseResult<PagedList<OrderProfitVO>> findDistOneOrderReferralsProfitSummary(@RequestBody OrderProfitSummaryCondition condition) {
        String logTitle = "/api-order/order/4806/v1/findDistOneOrderReferralsProfitSummary-一级分销商线下收益列表";
        LOGGER.info("{}=--开始--{}", logTitle, JsonUtil.toJSONString(condition));
        CustomerUser customer = UserContext.getCurrentCustomerUser();
        condition.setCustomerId(customer.getCustomerId());
        if (customer == null || customer.getCustomerId()==null) {
            throw new BusinessException(BusinessCode.CODE_1026);
        }
        //默认从第1页开始查询，每页20条
        if (condition.getPageNo() == DEFAULT_ZERO) {
            condition.setPageNo(DEFAULT_PAGE);
        }
        if (condition.getPageSize() == DEFAULT_ZERO) {
            condition.setPageSize(DEFAULT_PAGE_SIZE);
        }
        ResponseResult<PagedList<OrderProfitVO>> result = new ResponseResult<>();

        PagedList<OrderProfitVO> vo = orderDataStatisticsService.findDistOneOrderReferralsProfitSummary(condition);
        result.setData(vo);

        LOGGER.info("{}=--结束 result={}", logTitle, JsonUtil.toJSONString(result));
        return result;
    }


    @ApiOperation(value = "二级分销商查询数据", response = OrderDistributionTwoSummaryVO.class, notes = "二级分销商查询数据")
    @RequestMapping(value = "/4807/v1/getOrderDistributionTwoSummary", method = RequestMethod.POST)
    public ResponseResult<OrderDistributionTwoSummaryVO> getOrderDistributionTwoSummary(@RequestBody OrderSalesSummaryCondition condition) {
        String logTitle = "/api-order/order/4807/v1/getOrderDistributionTwoSummary-二级分销商查询数据";
        LOGGER.info("{}=--开始--{}", logTitle, JsonUtil.toJSONString(condition));
        CustomerUser customer = UserContext.getCurrentCustomerUser();
        condition.setCustomerId(customer.getCustomerId());
        if (customer == null || customer.getCustomerId()==null) {
            throw new BusinessException(BusinessCode.CODE_1026);
        }
        condition.setLevel(customer.getLevel());
        ResponseResult<OrderDistributionTwoSummaryVO> result = new ResponseResult<>();
        OrderDistributionTwoSummaryVO vo = orderDataStatisticsService.getOrderDistributionTwoSummary(condition);
        result.setData(vo);

        LOGGER.info("{}=--结束 result={}", logTitle, JsonUtil.toJSONString(result));
        return result;
    }

    @ApiOperation(value = "二级分销商总收益列表", response = OrderProfitVO.class, notes = "二级分销商总收益列表")
    @RequestMapping(value = "/4808/v1/findDistTwoOrderProfitSummary", method = RequestMethod.POST)
    public ResponseResult<PagedList<OrderProfitVO>> findDistTwoOrderProfitSummary(@RequestBody OrderProfitSummaryCondition condition) {
        String logTitle = "/api-order/order/4808/v1/findDistTwoOrderProfitSummary-二级分销商总收益列表";
        LOGGER.info("{}=--开始--{}", logTitle, JsonUtil.toJSONString(condition));
        CustomerUser customer = UserContext.getCurrentCustomerUser();
        condition.setCustomerId(customer.getCustomerId());
        if (customer == null || customer.getCustomerId()==null) {
            throw new BusinessException(BusinessCode.CODE_1026);
        }
        //默认从第1页开始查询，每页20条
        if (condition.getPageNo() == DEFAULT_ZERO) {
            condition.setPageNo(DEFAULT_PAGE);
        }
        if (condition.getPageSize() == DEFAULT_ZERO) {
            condition.setPageSize(DEFAULT_PAGE_SIZE);
        }
        ResponseResult<PagedList<OrderProfitVO>> result = new ResponseResult<>();

        PagedList<OrderProfitVO> vo = orderDataStatisticsService.findDistTwoOrderProfitSummary(condition);
        result.setData(vo);

        LOGGER.info("{}=--结束 result={}", logTitle, JsonUtil.toJSONString(result));
        return result;
    }

    @ApiOperation(value = "分销商总收益列表", response = OrderProfitVO.class, notes = "分销商总收益列表")
    @RequestMapping(value = "/4809/v1/findDistOrderProfitSummary", method = RequestMethod.POST)
    public ResponseResult<PagedList<OrderProfitVO>> findDistOrderProfitSummary(@RequestBody OrderProfitSummaryByLevelCondition condition) {
        String logTitle = "/api-order/order/4809/v1/findDistOrderProfitSummary-二级分销商总收益列表";
        LOGGER.info("{}=--开始--{}", logTitle, JsonUtil.toJSONString(condition));
        ResponseResult<PagedList<OrderProfitVO>> result = new ResponseResult<>();
        switch (condition.getUserLevel()) {
            case LEVEL_1:
                result = findDistOneOrderProfitSummary(condition);
                break;
            case LEVEL_2:
                result = findDistTwoOrderProfitSummary(condition);
                break;
            default:
                BusinessException e = new BusinessException();
                e.setErrorCode(BusinessCode.CODE_1001);
                throw e;
        }
        LOGGER.info("{}=--结束 result={}", logTitle, JsonUtil.toJSONString(result));
        return result;
    }

}
